﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.PovertyIn
{
    public partial class ULive : PUserControl
    {
        public void SaveData()
        {
          float f= 0.0f;
            float temp=0.0f;
            float.TryParse(tbDistrict.Text,out temp);
            f+=temp;
             temp=0.0f;
            float.TryParse(tbTown.Text,out temp);
            f+=temp;

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from SLiving where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [SLiving]
           ([Name],[Gender],[Address] ,[District],[Town] ,[Account] ,[PeopleCount]
           ,[Remark] ,[BelongTo],[Year],[HalfYear],[Money] ,[Type],[VillageName])
     VALUES
           ({0},'{1}',{2},{3},{4},{5},{6},{7},'{8}',{9},'{10}',{11},'{12}','{13}'); end else begin

UPDATE [SLiving]
   SET [Name] ={0}     ,[Gender] = '{1}'
      ,[Address] = {2}     ,[District] =  {3}
      ,[Town] =  {4}      ,[Account] = {5}
      ,[PeopleCount] =  {6}     ,[Remark] = {7}
      ,[BelongTo] =  '{8}',[Year] = {9}
      ,[HalfYear] = '{10}' ,[Money] = {11}
      ,[Type] = '{12}' ,[VillageName] =  '{13}'
 WHERE id=@id ; end", Util.ReturnSqlValue(tbName.Text),ddlGendar.SelectedValue,Util.ReturnSqlValue(tbAddress.Text),
                    Util.ReturnSqlValue(tbDistrict.Text),Util.ReturnSqlValue(tbTown.Text),Util.ReturnSqlValue(tbAcount.Text),
                    Util.ReturnSqlValue(tbPeopleCount.Text),Util.ReturnSqlValue(tbRemark.Text),ddlVillage.SelectedItem.Text,
                    Util.ReturnSqlValue(tbYear.Text),ddlHalf.SelectedValue,f,
                    ddlType.SelectedValue,ddlVillage.SelectedItem.Text    
                    );

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLPoverty.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [Sliving] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶贫帮困资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["Name"].ToString();
                tbAcount.Text = dr["account"].ToString();
                tbAddress.Text = dr["address"].ToString();
                tbDistrict.Text = dr["district"].ToString();
                tbTown.Text = dr["town"].ToString();
                tbPeopleCount.Text = dr["PeopleCount"].ToString();
                tbRemark.Text = dr["remark"].ToString();
                
              
                ListItem li = ddlVillage.Items.FindByText(dr["BelongTo"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;
                li = ddlHalf.Items.FindByValue(dr["HalfYear"].ToString());
                if (li != null)
                    ddlHalf.SelectedValue = li.Value;
                li = ddlType.Items.FindByText(dr["Type"].ToString());
                if (li != null)
                    ddlType.SelectedValue = li.Value;
                li = ddlGendar.Items.FindByText(dr["gender"].ToString());
                if (li != null)
                    ddlGendar.SelectedValue = li.Value;
            }
        }
    }
}